# Copyright (c) 2022, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from frappe.model.mapper import get_mapped_doc
from frappe.desk.form.load import get_attachments
from frappe.utils import (
	add_to_date, getdate, today
)


class SRMSampleRequestCaigou(Document):
	def autoname(self):
		if self.naming_series:
			self.name = self.naming_series


	def before_insert(self):
		if not self.naming_series:
			current_naming_series = "YPSQ-CG-"
			current_naming_series += "".join(today().split("-"))
			series = frappe.db.get_value('SRM Naming Series',
								{'doctype_name': self.doctype, 'date': today()},
								['name', 'current_series'], as_dict=True)
			if series:
				current_naming_series += str(series.current_series).zfill(3)
			else:
				frappe.get_doc({'doctype': 'SRM Naming Series', 'doctype_name': self.doctype, 'date': today()}).insert(ignore_permissions=True)
				current_naming_series += "001"
			self.naming_series = current_naming_series
			frappe.db.set_value('SRM Naming Series', series.name, 'current_series', series.current_series + 1)
		# 赋值一个字段以供发送邮件时使用
		if self.sample_state == "是":
			self.email_note = "送样时请按要求提供规格书及测试数据字样"


	def get_attachments(self):
		#通过当前单据及对就的上传字段名获取已上传文件名称/编号
		fname = frappe.db.get_value('File',
			{
				"attached_to_name": self.name,
				"attached_to_doctype": self.doctype,
				"attached_to_field": "attachment"
			})
		#获取文件单据对象
		file_doc = frappe.get_doc('File', fname)
		#填充发邮件时的附件参数，主要是获取pdf文件二进制内容
		attachments=[{"fname": file_doc.file_name,"fcontent": file_doc.get_content()}]
		return attachments

	
	@frappe.whitelist()
	def send_supplier_emails(self, rev_email):
		if rev_email:
			rev_email.append(self.applicant)
			docname = self.name
			material_number = self.material_number
			material_name = self.material_name
			technical_require =self.technical_require
			material_model = self.material_model
			qty = self.qty
			description = self.description
			require_date = self.require_date
			if self.attachment:
				cur_attachments = self.get_attachments()
				get_subject = f"{self.material_company_name}- 打样资料通知-有附件 - {docname} - {material_model}"
				frappe.sendmail(
					recipients=rev_email,
					sender=frappe.session.user,
					attachments=cur_attachments,
					reference_doctype=self.doctype,
					reference_name=docname,
					subject=get_subject,
					message=f"<p style='color:red;'> 本邮件属于系统发送-有附件，请勿直接回复 </p><br><p> {email_note}</p><br><p> 样品申请单号: {docname}</p><br><p> 样品名称: {material_name}</p><br><p> 规格型号: {material_model}</p><br><p> 技术要求: {technical_require}</p><br><p> 采购备注: {description}</p><br><p> 交期: {require_date}</p><br><p> 数量: {qty}</p><br><p> 如不能按期交付，请尽早回复。</p><br><p> 联系人: {self.applicant_name}</p><br><p> 联系方式: {self.applicant}</p><br>",
				)
			else:
				get_subject = f"{self.material_company_name}- 打样资料通知-无附件 - {docname} - {material_model}"
				frappe.sendmail(
					recipients=rev_email,
					sender=frappe.session.user,
					reference_doctype=self.doctype,
					reference_name=docname,
					subject=get_subject,
					message=f"<p style='color:red;'> 本邮件属于系统发送-无附件，请勿直接回复 </p><br><p> {email_note}</p><br><p> 样品申请单号: {docname}</p><br><p> 样品名称: {material_name}</p><br><p> 规格型号: {material_model}</p><br><p> 技术要求: {technical_require}</p><br><p> 采购备注: {description}</p><br><p> 交期: {require_date}</p><br><p> 数量: {qty}</p><br><p> 如不能按期交付，请尽早回复。</p><br><p> 联系人: {self.applicant_name}</p><br><p> 联系方式: {self.applicant}</p><br>",
				)
			
			self.admit_closed = 1
			self.save()
			self.reload()	